<template>
  <div>
    <el-image
        v-if="realSrcList.length>0"
        :src="`${realSrcList[0]}`"
        fit="contain"
        :style="`width:${realWidth};height:${realHeight};`"
        :preview-src-list="realSrcList"
        lazy
    >
      <div slot="error" class="image-slot">
        <i class="el-icon-picture-outline"></i>
      </div>
    </el-image>
    <el-empty v-else description="加载中" :image-size="(width<height?width:height)-20"/>
  </div>


</template>

<script>
import {listByIds} from "@/api/system/oss";

export default {
  name: "ImagePreview",
  props: {
    ossIds: {
      type: String,
      default: ""
    },
    width: {
      type: [Number, String],
      default: ""
    },
    height: {
      type: [Number, String],
      default: ""
    }
  },
  data() {
    return {
      realSrcList: []
    }
  },
  computed: {
    realWidth() {
      return typeof this.width == "string" ? this.width : `${this.width}px`;
    },
    realHeight() {
      return typeof this.height == "string" ? this.height : `${this.height}px`;
    }
  },
  mounted() {
    if (this.ossIds) {
      listByIds(this.ossIds)
          .then(res => {
            if (res.data) {
              this.realSrcList = res.data
                  .filter(img => img)
                  .map(img => img.url);
            }
          });
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-empty {
  padding: 0;
}

.el-image {
  border-radius: 5px;
  background-color: #ebeef5;
  box-shadow: 0 0 5px 1px #ccc;

  ::v-deep .el-image__inner {
    transition: all 0.3s;
    cursor: pointer;

    &:hover {
      transform: scale(1.2);
    }
  }

  ::v-deep .image-slot {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    color: #909399;
    font-size: 30px;
  }
}
</style>
